class Web::HomepagesController < ApplicationController

  # GET /admin/admin_users
  def index
    # 公司
    @company = Company.find_by(used: true)
    # 导航栏
    @naviga_bars = NavigaBar.where(company_id: @company.id).order(:order_number, :created_at)
    # 展示案例
    @technical_cores = TechnicalCore.where(company_id: @company.id).order(:order_number, :created_at)
    # 技术核心标题
    @naviga_bar_core_title = NavigaBar.find_by(key: 'priorities').name
    # 案例组标题
    @company_case_tile = NavigaBar.find_by(key: 'portfolio').name
    # 案例组列表
    @company_cases = CompanyCase.where(company_id: @company.id).order(:order_number, :created_at)
    # 关于我们
    @about_us_tile = NavigaBar.find_by(key: 'about').name
    # 轮播图
    @carousels = Carousel.where(company_id: @company.id).order(:order_number, :created_at)
    # 团队名称标题
    @team_title = NavigaBar.find_by(key: 'team').name
    # 团队
    @teams = Team.where(company_id: @company.id).order(:order_number, :created_at)
    # 联系我们标题
    @content_us_title = NavigaBar.find_by(key: 'contact').name
    # 联系客服
    @user_custom = UserCustom.order(:updated_at).first

    # 技术能力
    @own_powers = OwnPower.where(company_id: @company.id).order(:order_number, :created_at)

    # 常见问题
    @frequent_asks = FrequentAsk.where(company_id: @company.id).order(created_at: :desc)

    # 文章
    @articles = Article.where(company_id: @company.id).order(created_at: :desc).limit(3)

    @article_categories = ArticleCategory.where(cate_type: 'c_home').order(created_at: :desc)
  end

  def about_us
    # 公司
    @company = Company.find_by(used: true)
    # 联系客服
    @user_custom = UserCustom.order(:updated_at).first
    @article_categories = ArticleCategory.where(cate_type: 'c_home').order(created_at: :desc)

      # 展示案例
      @technical_cores = TechnicalCore.where(company_id: @company.id).order(:order_number, :created_at)
  end

  def contact
    # 公司
    @company = Company.find_by(used: true)
    # 联系客服
    @user_custom = UserCustom.order(:updated_at).first
    @article_categories = ArticleCategory.where(cate_type: 'c_home').order(created_at: :desc)

      # 展示案例
      @technical_cores = TechnicalCore.where(company_id: @company.id).order(:order_number, :created_at)
  end

  def blog
    # 公司
      # 展示案例

    @company = Company.find_by(used: true)
    @technical_cores = TechnicalCore.where(company_id: @company.id).order(:order_number, :created_at)
    # 联系客服
    @user_custom = UserCustom.order(:updated_at).first
    @article_categories = ArticleCategory.where(cate_type: 'c_home').order(created_at: :desc)

    if params[:search_title].present?
      @articles = Article.where("title like ?", "%#{params[:search_title]}%").page(params[:page]).per(5)
      @articel_count = @articles.count
    else
      if params[:cate].present?
        @category = ArticleCategory.find_by(id: params[:cate])
        @articel_count = @category.articles.count
        @articles = @category.articles.order(created_at: :desc).page(params[:page]).per(5)
      else
        @articles = Article.order(created_at: :desc)
        @articel_count = @articles.count
      end
    end

    @recent_articles = Article.order(:created_at).last(3)
    # 推荐分类
    @recen_article_categories = ArticleCategory.order(:created_at)
  end


  def blog_detail
    # 公司
    @company = Company.find_by(used: true)
    # 联系客服
    @user_custom = UserCustom.order(:updated_at).first
    @article_categories = ArticleCategory.where(cate_type: 'c_home').order(created_at: :desc)

    @article = Article.find_by(id: params[:id])

    @recent_articles = Article.order(:created_at).last(3)
    # 推荐分类
    @recen_article_categories = ArticleCategory.order(:created_at)

    @technical_cores = TechnicalCore.where(company_id: @company.id).order(:order_number, :created_at)
  end
end
